package top.yihoxu.client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import top.yihoxu.CurrentRequest;
import top.yihoxu.ResultResponse;
import top.yihoxu.entity.Avatar;
import top.yihoxu.entity.User;
import top.yihoxu.util.SignUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author dth
 * @Date 2023/8/11 21:51
 * @PackageName:com.yihoxu.yihoxu_interface.client
 * @ClassName: YihoxuClient
 * @Description:
 * @Version 1.0
 */
@Data
public class ApiClient {

    private String accessKey;
    private String secretKey;

    public ApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public ApiClient() {

    }

    public String getNameByGet(String name) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        String res = HttpUtil.get("http://localhost:8123/api/name/", hashMap);
        System.out.println(res);
        return res;
    }

    public String getNameByPost(String name) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        String res = HttpUtil.post("http://localhost:8123/api/name/post", hashMap);
        System.out.println(res);
        return res;
    }

    public String getUserNameByPost(User user) {
        String body = JSONUtil.toJsonStr(user);
        String userStr = JSONUtil.toJsonStr(user);
        HttpResponse response = HttpRequest.post("http://localhost:8123/api/name/user")
                .addHeaders(getHeadMap(body))
                .body(userStr)
                .execute();
        System.out.println(response.getStatus());
        String res = response.body();
        System.out.println(res);
        return res;
    }


    public Map<String, String> getHeadMap(String body) {
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put("accessKey", accessKey);
        // headMap.put("secretKey", this.accessKey);
        headMap.put("nonce", RandomUtil.randomNumbers(4));
        //headMap.put("timestamp", String.valueOf(System.currentTimeMillis()/1000));
        headMap.put("sign", SignUtils.getSign(headMap, secretKey));
        headMap.put("body", body);
        return headMap;
    }

    public ResultResponse sendRequest(CurrentRequest currentRequest, ApiClient apiClient) {
        String url = currentRequest.getUrl();
        String requestParams = currentRequest.getRequestParams();
        if (url.contains("name")) {
            NameClient nameClient = new NameClient();
            User user = nameClient.invoke(requestParams, apiClient);
            ResultResponse resultResponse = new ResultResponse();
            resultResponse.setCode(200);
            resultResponse.setMsg("成功");
            resultResponse.setData(user);
            return resultResponse;
        } else if (url.contains("avatar")) {
            AvatarClient avatarClient = new AvatarClient();
            Avatar avatar = avatarClient.invoke(requestParams, apiClient);
            ResultResponse resultResponse = new ResultResponse();
            resultResponse.setCode(200);
            resultResponse.setMsg("成功");
            resultResponse.setData(avatar);
            return resultResponse;
        }
        return null;
    }

}
